package org.ace.endingCode;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.handler.codec.MessageToMessageEncoder;

import AceCode.ByteArray;

public class AMF3NettyEncoder extends MessageToMessageEncoder<Object> {

	private void amfEncode(Object message, ByteArray out) {
		try {
			byte[] bs=AceCode.Code.aceEncode(message);
			out.WriteBytes(bs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void encode(io.netty.channel.ChannelHandlerContext ctx, Object msg,
			List<Object> out) throws Exception {
		if (msg instanceof ByteBuf) {
			out.add(((ByteBuf) msg).copy());
		} else {
			@SuppressWarnings("rawtypes")
			SocketModel model = (SocketModel) msg;
			ByteBuf buf = ByteBufUtil.threadLocalDirectBuffer();
			ByteArray arr=new ByteArray();

			arr.WriteInt(model.getOpDefinde());// 新加入的 协议ID 
//			arr.WriteInt(model.getType());
//			arr.WriteInt(model.getArea());
//			arr.WriteInt(model.getCommand());
			amfEncode(model.getMessage(), arr);
			buf.writeBytes(arr.getBuffer());
			arr=null;
			out.add(buf);
		}
		
	}
}
